package com.imooc.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Array;
import java.util.Map;

/**
 * Created by 廖师兄
 * 2017-07-04 01:30
 */
public class JsonUtil {
    
    /**
     * 实体对象到json
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        return gson.toJson(object);
    }
    
    /**
     * 从JSON字符串到实体类
     *
     *       Gson gson = new Gson();
     *       BDPushID bdPushID2 = gson.fromJson(bdPushId, BDPushID.class);
     *
     *
     *       {
     * "access_token": "20_RZAab-8fHkk2ba1Mxq0MAReZgB1ea_NLXhN942jM8uHB7m0hq6pNxKAd5BD7UekgsfX9vYcjeBXYjQ4_-sgJ4Q",
     * "expires_in": 7200,
     * "refresh_token": "20_JQweRVY3owDXIFR86DVfCJhb1tS6Rdmv9A9HiIqaCnh_hCH14IlR5gJAb010sCN2bn24u1ep_KU9qpjpKqErKg",
     * "openid": "oWpgJ5_wnyaLXuqSaH4XGMiH-2i8",
     * "scope": "snsapi_userinfo"
     * }
     * @param json
     * @return
     */
    
    
    public static Map toMap(String json) {
        Gson gson = new Gson();
        Map map = gson.fromJson(json, Map.class);
        return map;
    }
}
